package com.leetcode.p0;

public class Test27 {

    /**
     * 就是把2这个数字一直往后移
     * 2, 3, 4, 1
     * 3, 2, 4, 1
     * 3, 4, 2, 1
     * 3, 4, 1, 2
     */
    public int removeElement(int[] nums, int val) {
        int k = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                swap(nums, i, k);
                k++;
            }
        }
        return k;
    }

    public static void swap(int[] arr, int i, int j) {
        int t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public static void main(String[] args) {
        int[] data = new int[]{2, 3, 4, 1};

        Test27 test = new Test27();
        int count = test.removeElement(data, 2);

        for (int i = 0; i < count; i++) {
            System.out.println(data[i]);
        }
    }
}
